package org.cwnuit.train.service;

import java.util.ArrayList;
import java.util.List;

import org.cwnuit.train.dao.RulesDAO;
import org.cwnuit.train.dao.SworksDAO;
import org.cwnuit.train.domain.Rules;
import org.cwnuit.train.domain.Sworks;
import org.cwnuit.train.utils.PageModel;
import org.cwnuit.train.vo.NavVO;
import org.cwnuit.train.vo.SworkRuleVO;

@SuppressWarnings("all")
public class RulesService {
	
	RulesDAO rulesDAO = new RulesDAO();
	SworksDAO sworksDAO = new SworksDAO();

	//添加规章信息
	public boolean addRule(String SQL) {
		return rulesDAO.addRule(SQL);
	}
	
	//修改规章信息
	public boolean updateRule(String SQL) {
		return rulesDAO.updateRule(SQL);
	}
	
	//删除规章信息
	public boolean deleteRule(String SQL) {
		return rulesDAO.deleteRule(SQL);
	}
	
	//查询所有规章信息
	public List<Rules> findRules(String SQL) {
		return rulesDAO.findRules(SQL);
	}
	
	//查询规章信息
	public Rules findRule(String SQL) {
		return rulesDAO.findRule(SQL);
	}
	
	//分页查询规章信息
	public PageModel findByPage(int offset, int pagesize, String SQL) {
		return rulesDAO.findByPage(offset, pagesize, SQL);
	}
	
	//查询所有规章信息
	public List<NavVO> findNav() {
		
		String SQL1 = "SELECT * FROM Rules WHERE pid IS NULL ORDER BY rid ASC";
		
		List<NavVO> navlist = new ArrayList<NavVO>();
		List<Rules> rules1 = findRules(SQL1);
		for(Rules rule1 : rules1)
		{
			NavVO navVO1 = new NavVO();
			String SQL2 = "SELECT * FROM Rules WHERE pid = "+rule1.getRid()+" ORDER BY rid ASC";
			
			List<Rules> rules2 = findRules(SQL2);
			List<NavVO> navlist2 = new ArrayList<NavVO>();
			for (Rules rule2 : rules2) {
				NavVO navVO2 = new NavVO();
				String SQL3 = "SELECT * FROM Rules WHERE pid = "+rule2.getRid()+" ORDER BY rid ASC";
				
				List<Rules> rules3 = findRules(SQL3);
				List<NavVO> navlist3 = new ArrayList<NavVO>();
				for (Rules rule3 : rules3) {
					NavVO navVO3 = new NavVO();
					String SQL4 = "SELECT * FROM Rules WHERE pid = "+rule3.getRid()+" ORDER BY rid ASC";
					
					List<Rules> rules4 = findRules(SQL4);
					navVO3.setRule(rule3);
					navVO3.setRules(rules4);
					navlist3.add(navVO3);
				}
				navVO2.setRule(rule2);
				navVO2.setRuleList(navlist3);
				navlist2.add(navVO2);
			}
			navVO1.setRule(rule1);
			navVO1.setRuleList(navlist2);
			navlist.add(navVO1);
			
		}
		
		return navlist;
	}
	
	//查询所有规章信息并导出为word
	public List<NavVO> findNavToWord(int tid) {
		
		String SQL1 = "SELECT * FROM Rules WHERE pid IS NULL ORDER BY rid ASC";
		
		List<NavVO> navlist = new ArrayList<NavVO>();
		List<Rules> rules1 = findRules(SQL1);
		Sworks swork = null;
		for(Rules rule1 : rules1)
		{
			swork = sworksDAO.findSwork("SELECT * FROM Sworks WHERE tid = " + tid + " AND rid = " + rule1.getRid());
			NavVO navVO1 = new NavVO();
			String SQL2 = "SELECT * FROM Rules WHERE pid = "+rule1.getRid()+" ORDER BY rid ASC";
			
			List<Rules> rules2 = findRules(SQL2);
			List<NavVO> navlist2 = new ArrayList<NavVO>();
			Sworks swork1 = null;
			for (Rules rule2 : rules2) {
				swork1 = sworksDAO.findSwork("SELECT * FROM Sworks WHERE tid = " + tid + " AND rid = " + rule2.getRid());
				NavVO navVO2 = new NavVO();
				String SQL3 = "SELECT * FROM Rules WHERE pid = "+rule2.getRid()+" ORDER BY rid ASC";
				
				List<Rules> rules3 = findRules(SQL3);
				List<NavVO> navlist3 = new ArrayList<NavVO>();
				Sworks swork2 = null;
				for (Rules rule3 : rules3) {
					swork2 = sworksDAO.findSwork("SELECT * FROM Sworks WHERE tid = " + tid + " AND rid = " + rule3.getRid());
					NavVO navVO3 = new NavVO();
					String SQL4 = "SELECT * FROM Rules WHERE pid = "+rule3.getRid()+" ORDER BY rid ASC";
					List<Rules> rules4 = findRules(SQL4);
					
					List<Rules> rules4_new = new ArrayList<Rules>();
					Sworks swork3 = null;
					for(Rules rule4:rules4){
						swork3 = sworksDAO.findSwork("SELECT * FROM Sworks WHERE tid = " + tid + " AND rid = " + rule4.getRid());
						rule4.setSwork(swork3);
						rules4_new.add(rule4);
					}	
					rule3.setSwork(swork2);
					navVO3.setRule(rule3);
					navVO3.setRules(rules4_new);
					navlist3.add(navVO3);
				}
				rule2.setSwork(swork1);
				navVO2.setRule(rule2);
				navVO2.setRuleList(navlist3);
				navlist2.add(navVO2);
			}
			rule1.setSwork(swork);
			navVO1.setRule(rule1);
			navVO1.setRuleList(navlist2);
			navlist.add(navVO1);
			
		}
		
		return navlist;
	}
	
	
	
	//查询所有规章编制信息
	public PageModel listRuleSwork(int offset, int pagesize, String SQL,int tid) {
		
		List<SworkRuleVO> navlist = new ArrayList<SworkRuleVO>();
		PageModel pm = rulesDAO.findByPage(offset, pagesize, SQL);
		List<Rules> rulelist = pm.getDatas();
		SworkRuleVO srvo = null;
		Sworks swork = null;
		for(Rules rule : rulelist){
			srvo = new SworkRuleVO();
			srvo.setRule(rule);
			swork = sworksDAO.findSwork("SELECT * FROM Sworks WHERE tid = " + tid + " AND rid = " + rule.getRid());
			srvo.setSwork(swork);
			
			navlist.add(srvo);
		}
		pm.setDatas(navlist);
	    return pm;
	}

}
